<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item v-if="title=='新增'" :required="true" label="生产号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-request :ylimit="{key: true,label:'待领料'}" url="/production/productionTaskList/list" v-model="dataList.productionTaskListOdd" type="list" :trigger-change="true" @change="getData" />
                    </a-form-item>
                    <a-form-item v-else label="生产号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.productionTaskListOdd" placeholder="请输入生产号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-tag type="list" v-decorator="['warehouseId', validatorRules.warehouseId]" :trigger-change="true" dictCode="bas_warehouse,name,id" @sendName="getName" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="领料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入领料号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="下单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请输入下单日期" style="width: 100%" v-decorator="['date']" :trigger-change="true" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['state']" placeholder="待审核"></a-input>
                        <!-- <a-select ref="select" placeholder="请输入审批状态" v-decorator="['state']" >
                            <a-select-option value="待审核">待审核</a-select-option>
                            <a-select-option value="通过">通过</a-select-option>
                            <a-select-option value="不通过">不通过</a-select-option>
                        </a-select> -->
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="核算科目" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['accounting']" placeholder="请输入核算科目"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'
export default {
    name: "PickingModal",
    components: {
        JDate
    },
    props: ['odd'],
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            dataList: {},
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },
            confirmLoading: false,
            validatorRules: {
                warehouseId: {
                    rules: [{
                        required: true,
                        message: "请选择仓库!"
                    }]
                }
            },
            url: {
                add: "/production/picking/add",
                edit: "/production/picking/edit",
            }
        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        },
    },
    methods: {
        getName(value) {
            this.$set(this.dataList, 'warehouse', value)
        },
        add() {
            this.edit({});
        },
        async getData(value) {
            this.$set(this.dataList, 'productionTaskListOdd', value);
        },
        edit(record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            let {
                warehouse,
                productionTaskListOdd
            } = record;
            this.dataList = {
                warehouse,
                productionTaskListOdd,
                pickingType: '印刷'
            };
            this.odd ? this.$set(this.dataList, 'odd', this.odd) : this.$set(this.dataList, 'odd', record.odd)
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'warehouseId', 'date', 'state', 'accounting'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            if (this.dataList.productionTaskListOdd == '' || this.dataList.productionTaskListOdd == undefined) {
                this.$message.warning('请选择生产号!')
                return false;
            }
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    values = Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    console.log("表单提交数据", formData)
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                            that.$emit('_edit', formData)
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'warehouseId', 'date', 'state', 'accounting'))
        },

    }
}
</script>
